use bcrypt::{BcryptError, DEFAULT_COST};

#[inline]
pub fn hash(raw_password: &str) -> Result<String, BcryptError> {
    /// 在生成过程中使用了随机的salt, 随机的salt在生成的string中包含
    /// cost 调整加密的速度
    bcrypt::hash(raw_password, 6)
}

#[inline]
pub fn verify(raw_password: &str, encoded_password: &str) -> Result<bool, BcryptError> {
    bcrypt::verify(raw_password, encoded_password)
}

pub trait PasswordEncoder {
    fn encode(&self, raw_password: &str) -> String;
    fn matches(&self, raw_password: &str, encoded_password: &str) -> bool;
}

pub struct Bcrypt6Encoder;

impl PasswordEncoder for Bcrypt6Encoder {
    fn encode(&self, raw_password: &str) -> String {
        bcrypt::hash(raw_password, 6).unwrap()
    }

    fn matches(&self, raw_password: &str, encoded_password: &str) -> bool {
        bcrypt::verify(raw_password, encoded_password).unwrap()
    }
}
